﻿using System.Linq;
using System;
using System.Web;
using LINQExtensions;

namespace EasyLift.TMS.Web.Models
{
    public class MissionStatusRepository
    {
        private CoreBizDBEntities db = new CoreBizDBEntities();

        //Query Methods
        public IQueryable<MissionStatus> GetAllData()
        {
            return from MissionStatus in db.MissionStatuses
                   orderby MissionStatus.MissionStatusCode descending
                   select MissionStatus;
        }


        public IQueryable<MissionStatus> GetDataByName(string searchField, string searchOper, string searchString)
        {
            var ps = db.MissionStatuses.Where(p => p.MissionStatusCode.Contains(searchString));
            if (searchField == "MissionStatusDescription")
                ps = db.MissionStatuses.Where(p => p.MissionStatusDescription.Contains(searchString));
            if (searchField == "MissionStatusCode")
                ps = db.MissionStatuses.Where(p => p.MissionStatusCode.Contains(searchString));
            return ps.AsQueryable();

        }
        public IQueryable<MissionStatus> GetAllData(string sortColumn, string sortOrder, bool _search, string searchField, string searchOper, string searchString)
        {
            if (_search)
            {
                if (sortColumn != "")
                    if (sortOrder.ToUpper() == "DESC")
                        return GetDataByName(searchField, searchOper, searchString).OrderByDescending(sortColumn);
                    else
                        return GetDataByName(searchField, searchOper, searchString).OrderBy(sortColumn);
                else
                    return GetDataByName(searchField, searchOper, searchString).OrderBy(sortColumn);
            }
            else
            {
                if (sortColumn != "")
                    if (sortOrder.ToUpper() == "DESC")
                        return GetAllData().OrderByDescending(sortColumn);
                    else
                        return GetAllData().OrderBy(sortColumn);
                else
                    return GetAllData();
            }
        }



        public MissionStatus GetByID(string id)
        {
            return db.MissionStatuses.SingleOrDefault(MissionStatus => MissionStatus.MissionStatusCode == id);
        }

        //Inset
        public void Add(MissionStatus MissionStatus)
        {
            //if (MissionStatus.StatusID == 0 )
            //    MissionStatus.StatusID = 1;
            MissionStatus.InsertedUser = HttpContext.Current.User.Identity.Name;
            MissionStatus.InsertedDate = DateTime.Now;
            MissionStatus.UpdatedUser = HttpContext.Current.User.Identity.Name;
            MissionStatus.UpdatedDate = DateTime.Now;
            MissionStatus.DataVersion = 1;

            db.MissionStatuses.AddObject(MissionStatus);
        }

        //Update
        public void Update(MissionStatus MissionStatus)
        {

            MissionStatus originalMissionStatus = GetByID(MissionStatus.MissionStatusCode);

            MissionStatus.InsertedUser = originalMissionStatus.InsertedUser;
            MissionStatus.InsertedDate = originalMissionStatus.InsertedDate;
            MissionStatus.UpdatedUser = HttpContext.Current.User.Identity.Name;
            MissionStatus.UpdatedDate = DateTime.Now;
            MissionStatus.DataVersion = ++originalMissionStatus.DataVersion;
  
            db.MissionStatuses.ApplyCurrentValues(MissionStatus);
        }

        //Delete
        public void Delete(MissionStatus MissionStatus)
        {
            db.MissionStatuses.DeleteObject(MissionStatus);
        }

        //Persistance
        public void Save()
        {
            db.SaveChanges();
        }
    }
}